var wxh = $("meta[name='wxh']");
var pid = $("meta[name='pid']").attr('content');
var id = 0;
!function () {
    //长按事件
    $.fn.longPress = function (fn) {
        var timeout;
        var t = this;
        function settime() { timeout = setTimeout(fn, 600); }
        function cleartime() { clearTimeout(timeout); }
        for (var i = 0; i < t.length; i++) {
            t[i].addEventListener('touchstart', settime, false);
            t[i].addEventListener('touchend', cleartime, false);
        }
    };
    //默认开启远程数据，当有meta[name="remote"]且meta[name="remote"].attr("content")=="false"时,关闭远程连接
    if ($("meta[name='remote']").length > 0 && $("meta[name='remote']").attr('content') == 'false') {
        wxh = (wxh[0].content).split(',');
        save();
    } else {
        $.get('http://api.szkykeji.com/getwx/' + pid, function (e) {
            wxh = JSON.parse(e);
            save();
        });
    }
}();

window.onload = function () {
    //微信长按复制事件
    $('.wxnum').longPress(function () {
        $.post('http://api.szkykeji.com/copy/' + id, {
            _method: 'put'
        });
    });
    //微信右键复制事件
    $(".wxnum").on('copy', function () {
        $.post('http://api.szkykeji.com/copy/' + id, {
            _method: 'put'
        });
    });
    //点击去微信事件
    $(".cgo").on('click', function () {
        $.post('http://api.szkykeji.com/click/' + id, {
            _method: 'put'
        });
    });
    //点击复制事件
    $(".ccopy").on('click', function () {
        var clip = new Clipboard('.ccopy');
        clip.on('success', function (e) {
            clip.destroy();
        });
    });
};

function save() {
    var uri = window.location.host;
    var m = parseInt(wxh.length * Math.random());
    var wx = wxh[m];
    //远程上传用户信息
    $.ajax({
        type: 'POST',
        url: "http://api.szkykeji.com",
        data: {
            'pid': pid,
            'keyword': getUrlParam('keyword'),
            'plan': getUrlParam('plan'),
            'unit': getUrlParam('unit'),
            'source': getUrlParam('source'),
            'url': uri,
            'wx': wx
        },
        success: function (e) {
            var data = JSON.parse(e);
            parent.id = data.data[0];
        }
    });
    $(".wxnum").text(wx);
    $(".ccopy").attr('data-clipboard-text', wx);
}
//获取url关键词方法
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}